package com.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.utils.JDBCUtils;

import com.bean.Comment;

public class CommentDao {
	//查询当前文章message数量
    public int searchMessageNumber(int passageID) 
    		throws Exception,ClassNotFoundException, IllegalAccessException
            , InstantiationException, SQLException 
    {
    	Connection conn = null;
    	Statement stmt = null;
    	ResultSet rs = null;
    	String getEmail,getPassword;
		conn = JDBCUtils.getConnection();
		stmt = conn.createStatement();
		String sql = "SELECT COUNT(*) AS number FROM message WHERE passageID='"+passageID+"'";
		rs = stmt.executeQuery(sql);
		if(rs.next()) {
            int result=rs.getInt("number");
            stmt.close();
            conn.close();
            return result;
        }

        else {
            stmt.close();
            conn.close();
            return 0;
        }
    }
    
  //按照time的值返回message列表
    public Comment searchComment(Comment comment,int time,int passageID) 
    		throws Exception,ClassNotFoundException,
            IllegalAccessException, InstantiationException, SQLException 
    {
    	Connection conn = null;
    	Statement stmt = null;
    	ResultSet rs = null;
    	String getEmail,getPassword;
		conn = JDBCUtils.getConnection();
		stmt = conn.createStatement();
        String sql = "SELECT * FROM message WHERE passageID ='"+passageID+"' ";
		rs = stmt.executeQuery(sql);
		
		for(int i=1;i<time;i++) {
            if (rs != null)
                rs.next();
        }
        if(rs != null && rs.next()) {
            comment.setMsgName(rs.getString("msgName"));
            comment.setMsgDate(rs.getString("msgDate"));
            comment.setMsgContent(rs.getString("msgContent"));
            comment.setID(rs.getString("ID"));
        }

        rs.close();
        stmt.close();
        conn.close();
        return comment;
    }
    
  //添加评论
    public int message(String ID,String msgName,String msgDate,String msgContent,int passageID)
    		throws Exception,ClassNotFoundException, IllegalAccessException,
            InstantiationException, SQLException
    {
    	Connection conn = null;
    	Statement stmt = null;
    	ResultSet rs = null;
    	String getEmail,getPassword;
		conn = JDBCUtils.getConnection();
		stmt = conn.createStatement();
		String sql = "SELECT * FROM customer where ID ='"+ID+"'";
		rs = stmt.executeQuery(sql);
		
		String getMsgName;
        while(rs!=null&&rs.next()){

            getMsgName=rs.getString("name");
            stmt.execute(sql);


            String insertSQL="INSERT INTO message(msgName,msgDate,msgContent,passageID,ID) VALUES" +
                    "('" + getMsgName + "','" + msgDate + "','"+ msgContent + "',"+ passageID + ",'"+ ID + "');";

            stmt.execute(insertSQL);

            return 0;
        }
        return 1;
    }
    
  //用于处理用户点赞
    public boolean like(int passageID,String ID) 
    		throws Exception,ClassNotFoundException,
            IllegalAccessException, InstantiationException, SQLException
    {
    	Connection conn = null;
    	Statement stmt = null;
    	ResultSet rs = null;
    	String getEmail,getPassword;
		conn = JDBCUtils.getConnection();
		stmt = conn.createStatement();
		String sql = "SELECT * FROM likeSet WHERE passageID=" + passageID + " AND customerID='" + ID + "'";
		rs = stmt.executeQuery(sql);
		if(rs.next()==false)
        {
            String addSQL = "INSERT INTO likeSet(passageID,customerID) VALUES(" + passageID +",'"+ ID +"')";
            stmt.execute(addSQL);
            String addLike = "UPDATE passage SET likeNum=likeNum+1 WHERE passageID ='" + passageID + "'";
            stmt.executeUpdate(addLike);
            return true;
        }

        return false;
    }

}
